//
//  SaveViewController.m
//  A段项目,漫画
//
//  Created by dllo on 15/4/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SaveViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SaveViewController ()
@property(nonatomic,retain)UICollectionView *myCollection;
@property(nonatomic,retain)NSMutableArray *arr;
//动画
@property(nonatomic, retain)UIView *witeView;
@property(nonatomic, retain)NSMutableArray *witeImageArr;
@property(nonatomic, retain)UIImageView *witeImageView;
@property(nonatomic, retain)UIProgressView *myProgress;
@property(nonatomic, retain)UILabel *myLabel;
@end

@implementation SaveViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
        [MidTools shareMidTools].delegate10 = self;
    }
    return self;
}
- (void)createWiteImage{
    self.witeImageArr = [NSMutableArray array];
    NSArray *names = @[@"735d4677ec7cdd522281cfbddd29ac71－1（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－2（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－3（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－4（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－5（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－6（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－7（被拖移）.tiff", @"735d4677ec7cdd522281cfbddd29ac71－8（被拖移）.tiff",
        @"735d4677ec7cdd522281cfbddd29ac71－9（被拖移）.tiff",
        @"735d4677ec7cdd522281cfbddd29ac71－10（被拖移）.tiff",
        @"735d4677ec7cdd522281cfbddd29ac71－11（被拖移）.tiff",
        @"735d4677ec7cdd522281cfbddd29ac71－12（被拖移）.tiff",
        @"735d4677ec7cdd522281cfbddd29ac71－13（被拖移）.tiff",
        @"735d4677ec7cdd522281cfbddd29ac71－14（被拖移）.tiff",
        @"735d4677ec7cdd522281cfbddd29ac71－15（被拖移）.tiff",
        @"735d4677ec7cdd522281cfbddd29ac71－16（被拖移）.tiff"];
    
    
    for (NSString *name in names) {
        UIImage *image = [UIImage imageNamed:name];
        [self.witeImageArr addObject:image];
    }
    self.witeImageView.animationImages = self.witeImageArr;
    //设置播放时间
    self.witeImageView.animationDuration = 0.5;
}
- (void)createAvtivity{
    self.witeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.witeView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.witeView];
    [self.witeView release];
    CGFloat bili = 222.0 / 271;
    self.witeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, WIDTH / 1.5 * bili)];
    self.witeImageView.center = CGPointMake(WIDTH / 2, HEIGHT / 3);
    [self.witeView addSubview:self.witeImageView];
    [self.witeImageView release];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 1.5, 30)];
    self.myLabel.center = CGPointMake(WIDTH / 2, HEIGHT / 3 + WIDTH / 1.5 * bili / 2 + 10);
    self.myLabel.text = @"loading..........";
    [self.witeView addSubview:self.myLabel];
    self.myLabel.textAlignment = NSTextAlignmentCenter;
}

- (void)upDate{
    if ([MidTools shareMidTools].addSave != 0) {
        self.tabBarItem.badgeValue = [NSString stringWithFormat:@"+%ld",[MidTools shareMidTools].addSave];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createAvtivity];
    [self createWiteImage];
    [self creatView];
    
    [self dataPlease];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.title = @"精品推荐";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-zhankai"] style:UIBarButtonItemStylePlain target:self action:@selector(clickToCarSave)];
    
    [self.view bringSubviewToFront:self.witeView];
    //播放动画
    NSLog(@"播放");
    [self.witeImageView startAnimating];

}
#pragma mark 数据解析
-(void)dataPlease
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://mobilev3.ac.qq.com/Index/commendComicListForIos/page/1/listcnt/10/clientType/0/local_version/2.3.2" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *dataArr = [dic objectForKey:@"data"];
        for (NSMutableDictionary *tempDic in dataArr) {
            Recommendation *dation = [[Recommendation alloc]init];
            [dation setValuesForKeysWithDictionary:tempDic];
            [self.arr addObject:dation];
            [dation release];
        }
        [self.myCollection reloadData];
        NSLog(@"请求成功");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"数据解析失败");
        
    }];
}

//创建视图
-(void)creatView
{
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    
    flow.itemSize = CGSizeMake(WIDTH, HEIGHT);
    
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    self.myCollection = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:flow];
    self.myCollection.backgroundColor = [UIColor grayColor];
    self.myCollection.pagingEnabled = YES;
    self.myCollection.bounces = NO;
    
    //代理人
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    [self.view addSubview:self.myCollection];
    
    //注册
    [self.myCollection registerClass:[RecommCell class] forCellWithReuseIdentifier:@"reuse"];
    
}

#pragma mark 协议的两个方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RecommCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    Recommendation *dation = self.arr[indexPath.row];
    
    [cell.posterView sd_setImageWithURL:[NSURL URLWithString:dation.poster_cover_url] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        [self.view bringSubviewToFront:self.witeView];
        //播放动画
        [self.witeImageView startAnimating];
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        [self.witeImageView stopAnimating];
        [self.view bringSubviewToFront:self.myCollection];
    }];
    cell.lastupLabel.text = dation.lastup;
    cell.titleLable.text = dation.title;
    cell.briefLabel.text = dation.brief_intrd;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonDViewController *cdvc = [[CartoonDViewController alloc]init];
    Recommendation *dation = self.arr[indexPath.row];
    cdvc.comic_id = dation.comic_id;
    [self.navigationController pushViewController:cdvc animated:YES];
    [cdvc release];
}

//按钮的点击方法
-(void)clickToCarSave
{
    CartoonSaveViewController *csvc = [[CartoonSaveViewController alloc]init];
    [self.navigationController pushViewController:csvc animated:YES];
    [MidTools shareMidTools].addSave = 0;
    self.tabBarItem.badgeValue = nil;
    [csvc release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
